#include "Star.h"

Star::Star(D3DXVECTOR2 pos,EState state) : GameObject(pos,EObjectType::EStar,state)
{
	m_pSprite = new CSprite(ResourceManager::GetIns()->GetTexture(EResource::Star_ID),1,1,1,0,0);
	m_velocity = D3DXVECTOR2(0,0);
	m_direction = EDirection::NONE;
	hasJumped = false;
}

Star::~Star()
{
	GameObject::~GameObject();
}

void Star::Jump()
{
	m_velocity.y = STARSPEEDY;
	hasJumped = true;
}
void Star::Update(float dt)
{
	m_position += m_velocity*dt*60;
	if(m_position.y < -50)
		m_state = Die;

	if(m_direction==EDirection::Left)
	{
		m_velocity.x = -STARSPEEDX;
	}
	else if(m_direction == EDirection::Right)
	{
		m_velocity.x = STARSPEEDX;
	}

	if(hasJumped==true)
	{
		m_velocity.y -= 1.20f*dt*60;
	}

	if(m_position.y < -50)
	{
		m_state = EState::Die;
	}
}

void Star::UpdateCollision(list<GameObject*> listObjects, float dt)
{
	if(m_state == EState::NoCheckCollision)
		return;

	list<GameObject*>::iterator it;
	float moveX,moveY;	
	float normalx,normaly;
	float timeCols = 0;
	Box tmpBox = this->GetBox();

	for(it=listObjects.begin(); it!=listObjects.end(); ++it)
	{
		GameObject* otherObject = (*it);
		//neu doi tuong da chet thi bo qua
		if(otherObject->GetObjectState()==EState::Die || otherObject->GetObjectState()==EState::Breaking)
			continue;

		EObjectType typeOther=otherObject->GetObjectType();
		Box otherBox = otherObject->GetBox();

		if(otherObject == this)
			continue;

		if(AABB(tmpBox,otherBox,moveX,moveY) == false)
		{
			if(AABBCheck(GetSweptBroadphaseBox(tmpBox),otherBox)==true || AABBCheck(GetSweptBroadphaseBox(otherBox),tmpBox)==true)
			{
				tmpBox = this->GetBox();
				timeCols = SweptAABB(tmpBox,otherBox,normalx,normaly);
				if(timeCols > 0 && timeCols < 1)
				{
					timeCols*=dt;
					ECollisionDirect collDriect = GetCollisionDirect(normalx,normaly);
					float remainingtime = dt - timeCols;

					//////////////////////////////---Platform---//////////////////////////////////

					if(otherObject->GetKindOfObject()==ESolid)
					{
						switch(collDriect)
						{
						case Colls_Bot:
							{
								m_position.y += m_velocity.y*timeCols*60 + 1;
								Jump();
							}
							break;
						case Colls_Top:
							{
								m_position.y += m_velocity.y*timeCols*60;
								m_velocity.y = -m_velocity.y/2;
							}
							break;
						case Colls_Left:
							{
								m_position.x += m_velocity.x*timeCols*60 + 1;
								m_direction = EDirection::Right;
							}
							break;
						case Colls_Right:
							{
								m_position.x += m_velocity.x*timeCols*60 - 1;
								m_direction = EDirection::Left;
							}
							break;
						}
					}										
				}
				else //not collision sweap AABB
				{

				}
			}
		}
		else
		{
			if(otherObject->GetKindOfObject()==ESolid)
			{
				if(moveX<0)
				{
					m_position.x += moveX - 1;
					m_direction = EDirection::Left;
				}
				if( moveX > 0)
				{					
					m_position.x += moveX + 1;
					m_direction = EDirection::Right;
				}
				if(moveY !=0)
				{
					m_position.y += moveY+1;
					Jump();
				}
			}
		}
	}
}
